Preparing a TensorFlow model


In [1]:
import warnings
warnings.filterwarnings('ignore')

In [2]:
import tensorflow as tf
tf.logging.set_verbosity(tf.logging.ERROR)
print(tf.__version__)


1.10.0

In [3]:
!curl -O https://raw.githubusercontent.com/DJCordhose/ai/master/notebooks/manning/model/insurance.hdf5


  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100  175k  100  175k    0     0   487k      0 --:--:-- --:--:-- --:--:--  487k

In [4]:
model = tf.keras.models.load_model('insurance.hdf5')

In [5]:
!rm -rf tf

In [6]:
tf.keras.backend.set_learning_phase?

In [7]:
import os

export_path_base = 'tf'
version = 1
export_path = os.path.join(
      tf.compat.as_bytes(export_path_base),
      tf.compat.as_bytes(str(version)))

tf.keras.backend.set_learning_phase(0)
sess = tf.keras.backend.get_session()

classification_inputs = tf.saved_model.utils.build_tensor_info(model.input)
classification_outputs_scores = tf.saved_model.utils.build_tensor_info(model.output)

signature =  tf.saved_model.signature_def_utils.build_signature_def(
    inputs={'inputs': classification_inputs},
    outputs={'scores': classification_outputs_scores},
    method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME)

builder = tf.saved_model.builder.SavedModelBuilder(export_path)
builder.add_meta_graph_and_variables(
      sess, [tf.saved_model.tag_constants.SERVING],
      signature_def_map={
           tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: signature
      })
builder.save()


Out[7]:
b'tf\\1\\saved_model.pb'

In [8]:
!ls -l


total 6885
-rw-r--r-- 1 olive 197609    9293 Aug  8 10:59 0-generate.ipynb
drwxr-xr-x 1 olive 197609       0 Aug  8 10:58 data
drwxr-xr-x 1 olive 197609       0 Aug  8 12:01 figures
-rw-r--r-- 1 olive 197609  179704 Aug 14 13:23 insurance.hdf5
-rw-r--r-- 1 olive 197609   26822 Aug 14 09:34 insurance-customers-1500.csv
-rw-r--r-- 1 olive 197609  179704 Aug 13 19:06 insurance-simple.hdf5
drwxr-xr-x 1 olive 197609       0 Aug 13 18:59 model
-rw-r--r-- 1 olive 197609    3810 Aug 14 13:27 production preparation.ipynb
-rw-r--r-- 1 olive 197609      88 Aug  9 17:14 sample_insurance.json
drwxr-xr-x 1 olive 197609       0 Aug 14 13:27 tf
drwxr-xr-x 1 olive 197609       0 Aug 14 09:36 tfjs
-rw-r--r-- 1 olive 197609  634707 Aug  9 13:01 U3-M1-example.ipynb
-rw-r--r-- 1 olive 197609 3620457 Aug  9 10:31 U3-M2-nn-intro.ipynb
-rw-r--r-- 1 olive 197609 2002487 Aug  9 12:14 U3-M3-nn-no-bullshit.ipynb
-rw-r--r-- 1 olive 197609  338311 Aug 14 09:39 U3-M3-nn-simplified.ipynb
-rw-r--r-- 1 olive 197609    8054 Aug 14 13:22 U4-M1-Preparing TensorFlow models.ipynb
-rw-r--r-- 1 olive 197609   19871 Aug 14 12:26 U4-M2-Serving TensorFlow models.ipynb
-rw-r--r-- 1 olive 197609    9641 Aug 14 09:39 U4-M3-Deploying to the Browser using TensorFlow.js.ipynb

In [ ]: